from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response

from shiyanloutower.settings import FROM_EMAIL
from .myserializers import *
from django.core.mail import send_mail
from .utils import *
from .models import *
from .utils import Chuan_Email




# Create your views here.


# 注册接口
class Register(APIView):
    def post(self, request):
        email = request.data.get('email')
        password = request.data.get('password')
        verif = request.data.get('verif')

        # 检索收到邮件的数据是否失效
        vaildcode = ValidCode.objects.filter(email=email, code=verif, losetime__gte=time.time()).first()
        if not vaildcode:
            return Response({'code': 402, 'msg': '验证码已失效', 'data': ''})
        # 数据完整性
        if not all([email, password, verif]):
            return Response({'code': 201, 'msg': '数据不完整', 'data': ''})
        # 用户名
        nickname = request.data.get('nickname')
        if not nickname:
            nickname = email
        # 存入data
        data = {'email': email, 'password': password, 'verif': verif, 'nickname': nickname}
        # 反序列化添加数据
        user = UserUnserializer(data=data)
        if user.is_valid():
            user.save()

            # 清空验证码
            ValidCode.objects.filter(email=email).delete()
            return Response({'code': 200, 'msg': f'欢迎{email}加入实验楼', 'data': ''})
        return Response({'code': 400, 'msg': '网络错误', 'data': ''})


# 发送验证码接口
class SendEmail(APIView):
    def post(self, request):
        email = request.data.get('email')
        # 校验邮箱是否唯一
        obj = ValidCode.objects.filter(email=email).all()
        if obj:
            return Response({'code': 203, 'msg': '邮箱已存在', 'data': ''})

        # 如何获取验证码
        code = create_verify_code()

        # 校验验证码是否出现重复
        validcode = ValidCode.objects.filter(email=email,losetime__gte=time.time()).first()
        if validcode:
            return Response({'code': 205, 'msg': '请勿重复发送验证码', 'data': ''})

        data = {'email':email, 'code': code, 'losetime': time.time() + 300}

        # 反序列化添加验证码
        vaild = CodeUnserializer(data=data)

        res = Chuan_Email(code, email)

        if vaild.is_valid():
            vaild.save()
            if res:
                return Response({'code': 200, 'msg': '发送成功', 'data': ''})
            # else:
            #     return Response({'code': 401, 'msg': '发送失败', 'data': ''})
        return Response({'code': 400, 'msg': '网络错误', 'data': ''})


# 登录接口
class LoginAPIView(APIView):
    def post(self, request):
        token = request.data.get('token')
        is_login = request.data.get('is_login')
        email = request.data.get('email')
        password = request.data.get('password')
        data = {'email': email, 'password': password}
        password = encrypt(data.pop('password'))
        user = User.objects.filter(email=email, password=password).first()
        data = {
            'token':token,
            'email': email,
            'nickname': is_login
        }
        if user:
            return Response({'code': 200, 'msg': '登录成功', 'uid': user.id, 'nickname': user.email, 'data': data})
        return Response({'code': 400, 'msg': '网络错误'})

